from microbit import *

class LED:
    def __init__(self):
        self.w = self.h = 5

    # 点亮x,y
    def turn_on_pixel(self, x, y, b=9):
        display.set_pixel(x, y, b)

    # 熄灭x,y
    def turn_off_pixel(self, x, y):
        display.set_pixel(x, y, 0)

    # 切换x,y
    def toggle_pixel(self, x, y):
        b = display.get_pixel(x, y)
        if b > 0:
            self.turn_off_pixel(x, y)
        else:
            self.turn_on_pixel(x, y, 9)

    # x,y是亮灯状态
    def is_pixel_on(self, x, y):
        return display.get_pixel(x, y) > 0

    # 绘制折线图
    def plot_bar_graph(self, value, high):
        bgh = 0
        if high > 0:
            bgh = high
        elif value > bgh:
            bgh = value

        v = value / bgh
        dv = 1 / 16
        k = 0

        for y in range(4, -1, -1):
            for x in range(3):
                if k > v:
                    self.turn_off_pixel(2 - x, y)
                    self.turn_off_pixel(2 + x, y)
                else:
                    self.turn_on_pixel(2 - x, y)
                    led.turn_on_pixel(2 + x, y)
                    k += dv
led = LED()